//index.js
//获取应用实例
const app = getApp()
let end = false
Page({
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },

  data: {
    background: [{
        id: 1,
        url: 'pages/detail/detail',
        image: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3911543902,2446960458&fm=26&gp=0.jpg"
      },
      {
        id: 2,
        url: 'pages/detail/detail',
        image: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3911543902,2446960458&fm=26&gp=0.jpg"
      },
      {
        id: 3,
        url: 'pages/detail/detail',
        image: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3911543902,2446960458&fm=26&gp=0.jpg"
      }
    ],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    currentIndex: 0,
    currentIndex1: 1,
    categories: [{
        id: 1,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 2,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 3,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 4,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 5,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 6,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 7,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 8,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 9,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 10,
        name: '水果馆',
        image: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3519921324,2682679311&fm=26&gp=0.jpg"
      },
      {
        id: 11,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 12,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 13,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 14,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 15,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 16,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 17,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 18,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 19,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 20,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 21,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      },
      {
        id: 22,
        name: '水果馆',
        image: "https://img.yzcdn.cn/vant/leaf.jpg"
      }
    ],
    lists: [{
        id: 1,
        title: '本商品由长沙森世达商贸有限公司专供',
        video: '',
        info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
        popularity: '6.7万',
        presaletime: 1564624526889,
        deliverytime: 1564624526889,
        limitnum: 2500,
        begintime: 1564624526889,
        price: 14.99,
        oldprice: 16,
        buynum: '1.2万',
        page: 1,
        ending: false
      },
      {
        id: 2,
        title: '本商品由长沙森世达商贸有限公司专供',
        video: '',
        info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
        popularity: '6.7万',
        presaletime: 1564624526889,
        deliverytime: 1564624526889,
        limitnum: 2500,
        begintime: 1564624526889,
        price: 14.99,
        oldprice: 16,
        buynum: '1.2万',
        page: 1,
        ending: false
      },
    ],
    lists2: [{
        id: 3,
        title: '本商品由长沙森世达商贸有限公司专供',
        video: '',
        info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
        popularity: '6.7万',
        presaletime: 1564624526889,
        deliverytime: 1564624526889,
        limitnum: 2500,
        begintime: 1564624526889,
        price: 14.99,
        oldprice: 16,
        buynum: '1.2万',
        page: 1,
        ending: false
      },
      {
        id: 4,
        title: '本商品由长沙森世达商贸有限公司专供',
        video: '',
        info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
        popularity: '6.7万',
        presaletime: 1564624526889,
        deliverytime: 1564624526889,
        limitnum: 2500,
        begintime: 1564624526889,
        price: 14.99,
        oldprice: 16,
        buynum: '1.2万',
        page: 1,
        ending: false
      },
    ]
  },
  onLoad() {
    let clen = Math.ceil((this.data.categories.length) / 10)
    console.log(clen)
    let arr = [...this.data.categories]
    let categories = []
    for (let i = 0; i < clen; i++) {
      categories[i] = arr.slice(10 * i, 10 * (i + 1))
    }
    this.setData({
      categories
    })
    console.log(categories)
    const lists = this.data.lists
    for (let i = 0; i < lists.length; i++) {
      lists[i].presaletime = this.getMD(lists[i].presaletime);
      lists[i].deliverytime = this.getMD(lists[i].deliverytime);
      lists[i].begintime = this.getMD(lists[i].begintime, 'hour')
    }
    this.setData({
      lists: [...lists]
    })
  },
  change1(e) {
    //console.log(e.detail)
    this.setData({
      currentIndex1: e.detail.current + 1
    })
  },
  change(e) {
    console.log(e.detail)
    this.setData({
      currentIndex: e.detail.current
    })
  },
  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  onReachBottom: function (e) {
    wx.showLoading({
      title: '加载中',
    })
    if (end) {
      return
    }
    console.log('onReachBottom')
    const lists = this.data.lists
    const lists2 = this.data.lists2
    for (let i = 0; i < lists2.length; i++) {
      lists2[i].presaletime = this.getMD(lists2[i].presaletime);
      lists2[i].deliverytime = this.getMD(lists2[i].deliverytime);
      lists2[i].begintime = this.getMD(lists2[i].begintime, 'hour')
    }
    this.setData({
      lists: [...lists, ...lists2]
    })
    wx.hideLoading({})
    end = true
  },
  getMD(timestamp, hour) {
    if (hour) {
      let time = new Date(timestamp)
      let month = time.getMonth() + 1
      let date = time.getDate()
      let hours = time.getHours()
      if (month < 10) {
        month = '0' + month
      }
      if (date < 10) {
        date = '0' + date
      }
      if (hours < 10) {
        hours = '0' + hours
      }
      return month + '月' + date + '日' + hours + '点'
    }
    let time = new Date(timestamp)
    let month = time.getMonth() + 1
    let date = time.getDate()
    if (month < 10) {
      month = '0' + month
    }
    if (date < 10) {
      date = '0' + date
    }
    return month + '月' + date + '日'
  }
})